<template>
  <vxe-modal
    ref="commonSelectModal"
    v-model="visible"
    :resize="true"
    width="1024"
    height="600"
    title="选择数据源"
    :show-zoom="true"
    :destroy-on-close="false"
    :show-footer="true"
    :mask="false"
    @close="cancel"
  >
    <div class="common-select-container">
      <div class="searchBox">
        <el-form :inline="true">
          <el-form-item label="名称：" prop="name">
            <el-input v-model="search.name" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="编码" prop="code">
            <el-input v-model="search.code" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item style="margin-top: -2px">
            <el-button type="primary" @click="tableSearch">查询</el-button>
            <el-button
              type="info"
              @click="(search = {}), tableSearch()"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <qm-table
        v-if="visible"
        ref="qmTable"
        :urls="{ query: '/smartform/smartTable/query' }"
        :show-menu="false"
        :show-toolbar="false"
        :show-checkbox="multiple"
        :show-radio="!multiple"
        :options="calOptions"
        :pager-options="{ pageSize: 5 }"
        :before-query="beforeQuery"
      >
        <qm-table-column field="name" sortable>名称</qm-table-column>
        <qm-table-column field="code" sortable>编码</qm-table-column>
      </qm-table>
    </div>

    <template #footer>
      <el-row>
        <el-col :span="24" style="text-align: right;">
          <el-button type="primary" @click="returnResult">确定</el-button>
          <el-button type="info" @click="cancel">关闭</el-button>
        </el-col>
      </el-row>
    </template>
  </vxe-modal>
</template>

<script>
import _ from 'lodash'

export default {
  name: 'SelectTable',
  props: {
    url: String,
    multiple: Boolean,
    value: Boolean,
    onBeforeQuery: Function
  },
  data() {
    return {
      search: {},
      visible: false
    }
  },
  computed: {
    calOptions() {
      if (this.multiple) {
        return { checkboxConfig: { highlight: true, trigger: 'row' }}
      }
      return {
        checkboxConfig: { highlight: false },
        radioConfig: { highlight: true, trigger: 'row' }
      }
    }
  },
  watch: {
    value(visible) {
      this[visible ? 'show' : 'hide']()
    }
  },
  methods: {
    tableSearch() {
      const params = {
        start: 0,
        query: {}
      }
      const search = _.clone(this.search)
      params.query = _.extend({}, search)
      this.$refs['qmTable'].fetchListData(params)
    },
    beforeQuery(params) {
      if (this.onBeforeQuery) {
        this.onBeforeQuery(params)
      } else {
        params.query = _.extend({}, this.search)
      }
    },
    show() {
      this.visible = true
    },
    hide() {
      this.visible = false
    },
    cancel() {
      this.$emit('cancel')
    },
    returnResult() {
      const rows = this.$refs['qmTable'].getSelectedRows()
      this.$emit('success', rows)
    }
  }
}
</script>

<style></style>
